{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d15a361e",
   "metadata": {},
   "source": [
    "# python 使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "52af2fb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "7c1de396",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'   # 我主要用到的是Detect API 调用URL:https://api-cn.faceplusplus.com/facepp/v3/detect\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc231c85",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "9bd0108f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "f36321ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1648279060,5d0d4015-1ac9-4c3c-b6ff-02529a72fd64\",\"time_used\":545,\"faces\":[{\"face_token\":\"3957bd415680fe0928002ef4ef73c5be\",\"face_rectangle\":{\"top\":108,\"left\":141,\"width\":107,\"height\":107}}],\"image_id\":\"JoYdaZ0h1hGb4ih1d2EZVw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "84a60d96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3957bd415680fe0928002ef4ef73c5be'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "81afe76a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# face_token的变量6bf6fa79f91c26a88f97e9f6edc839a9 ---> 传递给了'face_tokens'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1225a327",
   "metadata": {},
   "source": [
    "### 关于  face_token 是人脸的ID   （实际上，只要我们请求一次，就会成功开通一次，face++）\n",
    "1. 如果您需要将检测出的人脸用于后续的分析、比对等操作，建议将对应的 face_token 添加到 FaceSet 中。如果一个 face_token 在 72 小时内没有存放在任一 FaceSet 中，则该 face_token 将会失效。<br>如果对同一张图片进行多次人脸检测，同一个人脸得到的 face_token 是不同的。</br>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6a92b247",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648279060,5d0d4015-1ac9-4c3c-b6ff-02529a72fd64',\n",
       " 'time_used': 545,\n",
       " 'faces': [{'face_token': '3957bd415680fe0928002ef4ef73c5be',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c1420ab",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1aec8e30",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7464966",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "526ac08c",
   "metadata": {},
   "source": [
    "#  人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "87fef665",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'   \n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "   'face_tokens':face_token,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "}\n",
    "\n",
    "r= requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "3214b1ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "b26bf04b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 160,\n",
       " 'request_id': '1648279067,cc2208e0-2477-42b6-8c3e-fedcaf1e4464',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.681,\n",
       "     'neutral': 0.007,\n",
       "     'disgust': 1.213,\n",
       "     'anger': 0.007,\n",
       "     'surprise': 0.009,\n",
       "     'fear': 0.112,\n",
       "     'happiness': 97.97},\n",
       "    'beauty': {'female_score': 56.631, 'male_score': 56.694},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'mouthstatus': {'close': 0.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 100.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 1.982,\n",
       "     'stain': 12.624,\n",
       "     'acne': 0.634,\n",
       "     'health': 9.525},\n",
       "    'headpose': {'yaw_angle': 0.004189467,\n",
       "     'pitch_angle': 16.763775,\n",
       "     'roll_angle': 2.3050764},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.364},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.364},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.364}},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.002,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.998,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.031,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.008,\n",
       "      'no_glass_eye_open': 99.961,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 85.658},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.468,\n",
       "      'vector_z_component': 0.944,\n",
       "      'vector_x_component': -0.054,\n",
       "      'vector_y_component': 0.325,\n",
       "      'position_y_coordinate': 0.443},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.506,\n",
       "      'vector_z_component': 0.948,\n",
       "      'vector_x_component': 0.168,\n",
       "      'vector_y_component': 0.269,\n",
       "      'position_y_coordinate': 0.436}}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': '3957bd415680fe0928002ef4ef73c5be'}]}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcc8614b",
   "metadata": {},
   "source": [
    "## 数据处理小魔法 pandas.json_normalize()实现数据表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "79fdef46",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "42238b6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>3957bd415680fe0928002ef4ef73c5be</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.681</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>0.007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>1.213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>97.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>56.631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>56.694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>1.982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>12.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>0.634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>9.525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>0.004189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>16.763775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>2.305076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>85.658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.944</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>0.325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.443</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.506</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>0.168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>0.269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          3957bd415680fe0928002ef4ef73c5be\n",
       "attributes.emotion.sadness                                                     0.681\n",
       "attributes.emotion.neutral                                                     0.007\n",
       "attributes.emotion.disgust                                                     1.213\n",
       "attributes.emotion.anger                                                       0.007\n",
       "attributes.emotion.surprise                                                    0.009\n",
       "attributes.emotion.fear                                                        0.112\n",
       "attributes.emotion.happiness                                                   97.97\n",
       "attributes.beauty.female_score                                                56.631\n",
       "attributes.beauty.male_score                                                  56.694\n",
       "attributes.gender.value                                                         Male\n",
       "attributes.age.value                                                              53\n",
       "attributes.mouthstatus.close                                                     0.0\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                    100.0\n",
       "attributes.mouthstatus.other_occlusion                                           0.0\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                              1.982\n",
       "attributes.skinstatus.stain                                                   12.624\n",
       "attributes.skinstatus.acne                                                     0.634\n",
       "attributes.skinstatus.health                                                   9.525\n",
       "attributes.headpose.yaw_angle                                               0.004189\n",
       "attributes.headpose.pitch_angle                                            16.763775\n",
       "attributes.headpose.roll_angle                                              2.305076\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                 0.364\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                               0.364\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                             0.364\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                         100.0\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                             0.002\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                   0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                            99.998\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                             0.031\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                0.008\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                            99.961\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                               0.0\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  85.658\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.468\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.944\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.054\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                             0.325\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.443\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.506\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.948\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                             0.168\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                             0.269\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.436\n",
       "face_rectangle.width                                                             107\n",
       "face_rectangle.top                                                               108\n",
       "face_rectangle.left                                                              141\n",
       "face_rectangle.height                                                            107"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cbd65c6",
   "metadata": {},
   "source": [
    "# 人脸对比\n",
    "* 人脸对比至少要两张脸"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25128a5c",
   "metadata": {},
   "source": [
    "## 人脸检测获取token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "b687eccd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'   # 我主要用到的是Detect API 调用URL:https://api-cn.faceplusplus.com/facepp/v3/detect\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "971027a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'65a2ae8751abd0295a31e297dca073e1'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "e71397d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'   # 我主要用到的是Detect API 调用URL:https://api-cn.faceplusplus.com/facepp/v3/detect\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'    \n",
    "}\n",
    "\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "b9f39809",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a13b9f1230bf1a2d33f7e1ae54a06d79'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4fce724",
   "metadata": {},
   "source": [
    "## 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "9937b4ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'   # 我主要用到的是Detect API 调用URL:https://api-cn.faceplusplus.com/facepp/v3/detect\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02,\n",
    "}\n",
    "\n",
    "r = requests.post(url = compare_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "458ed3fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "cd689ceb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648279181,8db25ab9-fdb9-4256-b4e4-9f90e240e57d',\n",
       " 'time_used': 577,\n",
       " 'confidence': 83.194,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8eb82ecf",
   "metadata": {},
   "source": [
    "> **'confidence': 97.389**\n",
    ">> * confidence ---> 类型：Float\n",
    "<br><br>\n",
    "比对结果置信度，范围 [0,100]，小数点后3位有效数字，数字越大表示两个人脸越可能是同一个人。\n",
    "<br>\n",
    "注：如果传入图片但图片中未检测到人脸，则无法进行比对，本字段不返回。\n",
    "\n",
    "\n",
    "> **'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}**\n",
    ">> * thresholds ----> 类型：Object\n",
    "<br><br>\n",
    "一组用于参考的置信度阈值，包含以下三个字段。每个字段的值为一个 [0,100] 的浮点数，小数点后 3 位有效数字。\n",
    "<br><br>\n",
    "1e-3：误识率为千分之一的置信度阈值；\n",
    "<br><br>\n",
    "1e-4：误识率为万分之一的置信度阈值；\n",
    "<br><br>\n",
    "1e-5：误识率为十万分之一的置信度阈值；\n",
    "<br><br>\n",
    "如果置信值低于“千分之一”阈值则不建议认为是同一个人；如果置信值超过“十万分之一”阈值，则是同一个人的几率非常高。\n",
    "<br><br>\n",
    "请注意：阈值不是静态的，每次比对返回的阈值不保证相同，所以没有持久化保存阈值的必要，更不要将当前调用返回的 confidence 与之前调用返回的阈值比较。\n",
    "<br><br>\n",
    "注：如果传入图片但图片中未检测到人脸，则无法进行比对，本字段不返回。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ce88575",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "7c022693",
   "metadata": {},
   "source": [
    "# 封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "416649a5",
   "metadata": {},
   "source": [
    "* 准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "ca9acd89",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "img_url_01 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "ff8e0db3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "e3fd54bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# def detect_face(API_key,API_secret,img_url):\n",
    "#     \"\"\"face++ 人脸检测接口函数(图片链接)\"\"\"\n",
    "#     base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "#     payload = {\n",
    "#         'api_key':API_key,\n",
    "#         'api_secret':API_sercret,\n",
    "#         'image_url':img_url,\n",
    "#         'return_landmark':return_landmark,\n",
    "#         'return_attributes':return_attributes,\n",
    "#         'face_rectangle':face_rectangle,\n",
    "#         'beauty_score_min':beauty_score_min,\n",
    "#         'beauty_score_max':beauty_score_max   \n",
    "#     }\n",
    "#     r = requests.post(url = base_url, params = payload)\n",
    "#     return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "902daaa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648281673,11294fc0-006e-4140-b6c0-241fb012c185',\n",
       " 'time_used': 533,\n",
       " 'faces': [{'face_token': '3d3b772347314efcb7a2a6e3ab302ac5',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "c75851a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648281676,22d3f3f4-fef5-4737-b792-e8537969bf8f',\n",
       " 'time_used': 401,\n",
       " 'faces': [{'face_token': 'a7f950ee2a6e7882258712eed317afe2',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27eedbb4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a65786d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
